﻿using java.applet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using ScriptCoreLib.JavaScript.Extensions;
using ScriptCoreLib.JavaScript.DOM.HTML;
using System.Dynamic;

namespace com.abstractatech.gamification.tdjdosboxe.Library
{
    [Description("To be used from JavaScript")]
    static class AppletExtensions
    {
        public static AppletParamDynamicObject AsAppletParamDynamicObject<T>(this T that) where T : Applet
        {
            return new AppletParamDynamicObject { Context = that };
        }

        public static T AddAppletParam<T>(this T that, string name, string value) where T : Applet
        {
            var e = that.ToHTMLElement();

            new IHTMLParam { name = name, value = value }.AttachTo(e);

            return that;
        }
    }

    public class AppletParamDynamicObject : DynamicObject
    {
        public Applet Context;

        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            Context.AddAppletParam(binder.Name, "" + value);

            return true;
        }
    }
}
